<?php

namespace App\DB;

use DB;
use App\Exceptions\ReqException;

class DeviceDB {
	
	/**
	 * 获取设备信息
	 *
	 * @param unknown $device_no        	
	 */
	public static function getDeviceInfo($uid, $device_no) {
		$sql = 'select a.device_no, a.device_type, a.uid, a.address, a.`status`, a.critical_value1, a.critical_value2, a.critical_value3, a.critical_value4, a.critical_value5, a.critical_value6, b.company, a.created_at from devices a, sys_users b ';
		$condition = 'where a.uid = b.uid and a.device_no = ? ';
		$params = [ 
				$device_no 
		];
		if (! array_contain_value ( PermissionDB::getUserRoles ( $uid ), '02' ) && ! array_contain_value ( PermissionDB::getUserRoles ( $uid ), '01' )) {
			$condition .= 'and a.uid = ? ';
			$params [] = $uid;
		}
		$devices = DB::select ( $sql . $condition, $params );
		if (count ( $devices ) == 0) {
			return [ ];
		}
		return $devices [0];
	}
	
	/**
	 * 分页获取设备信息列表
	 *
	 * @param unknown $uid        	
	 * @param unknown $page        	
	 * @param unknown $size        	
	 */
	public static function getDeviceList($uid, $page, $size) {
		$sql = 'select device_no, device_type, uid, address, `status`, critical_value1, critical_value2, critical_value3, critical_value4, critical_value5, critical_value6 from devices ';
		$params = [ ];
		$condition = 'where 1=1 ';
		
		if (! array_contain_value ( PermissionDB::getUserRoles ( $uid ), '02' )) {
			$condition .= 'and uid = ? ';
			$params [] = $uid;
		}
		$condition .= 'order by id desc limit ?, ?';
		$num_page = intval ( $page );
		$num_size = intval ( $size );
		$params [] = ($num_page - 1) * $num_size;
		$params [] = $num_size;
		
		return DB::select ( $sql . $condition, $params );
	}
	public static function getDeviceListCount($uid) {
		$sql = 'select count(*) as `count` from devices ';
		$params = [ ];
		$condition = 'where 1=1 ';
		
		$user = UserDB::getUserByUid ( $uid );
		if ($user->level !== '1') {
			$condition .= 'and uid = ? ';
			$params [] = $uid;
		}
		$result = DB::select ( $sql . $condition, $params );
		return $result [0]->count;
	}
	/**
	 * 添加设备
	 *
	 * @param unknown $device_no        	
	 * @param unknown $device_type        	
	 * @param unknown $uid        	
	 * @param unknown $address        	
	 * @param unknown $critical_value1        	
	 * @param unknown $critical_value2        	
	 * @param unknown $critical_value3        	
	 * @param unknown $critical_value4        	
	 */
	public static function addDevice($device) {
		$sql = 'insert into devices(device_no, device_type, uid, address, `status`, critical_value1, critical_value2, critical_value3, critical_value4, critical_value5, critical_value6, created_at, updated_at) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$params = [ 
				$device->device_no,
				$device->device_type,
				$device->uid,
				$device->address,
				'01',
				$device->critical_value1,
				$device->critical_value2,
				$device->critical_value3,
				$device->critical_value4,
				$device->critical_value5,
				$device->critical_value6,
				date ( "Y-m-d H:i:s" ),
				date ( "Y-m-d H:i:s" ) 
		];
		return DB::insert ( $sql, $params );
	}
	/**
	 * 删除设备
	 *
	 * @param unknown $device_no        	
	 */
	public static function deleteDevice($device) {
		$sql = 'delete from devices where device_no = ?';
		$params = [ 
				$device->device_no 
		];
		return DB::delete ( $sql, $params );
	}
	/**
	 * 更新设备
	 *
	 * @param unknown $device_no        	
	 * @param unknown $uid        	
	 * @param unknown $address        	
	 * @param unknown $status        	
	 * @param unknown $critical_value1        	
	 * @param unknown $critical_value2        	
	 * @param unknown $critical_value3        	
	 * @param unknown $critical_value4        	
	 */
	public static function updateDevice($device) {
		$sql = 'update devices set ';
		$params = [ ];
		if (! is_empty_or_null_string ( $device->uid )) {
			$sql .= 'uid = ?, ';
			$params [] = $device->uid;
		}
		if (! is_empty_or_null_string ( $device->address )) {
			$sql .= 'address = ?, ';
			$params [] = $device->address;
		}
		if (! is_empty_or_null_string ( $device->status )) {
			$sql .= '`status` = ?, ';
			$params [] = $device->status;
		}
		if (is_num_or_numstr ( $device->critical_value1 )) {
			$sql .= 'critical_value1 = ?, ';
			$params [] = $device->critical_value1;
		}
		if (is_num_or_numstr ( $device->critical_value2 )) {
			$sql .= 'critical_value2 = ?, ';
			$params [] = $device->critical_value2;
		}
		if (is_num_or_numstr ( $device->critical_value3 )) {
			$sql .= 'critical_value3 = ?, ';
			$params [] = $device->critical_value3;
		}
		if (is_num_or_numstr ( $device->critical_value4 )) {
			$sql .= 'critical_value4 = ?, ';
			$params [] = $device->critical_value4;
		}
		if (is_num_or_numstr ( $device->critical_value5 )) {
			$sql .= 'critical_value5 = ?, ';
			$params [] = $device->critical_value5;
		}
		if (is_num_or_numstr ( $device->critical_value6 )) {
			$sql .= 'critical_value6 = ?, ';
			$params [] = $device->critical_value6;
		}
		$sql = substr ( $sql, 0, strlen ( $sql ) - 2 );
		$sql .= ' where device_no = ?';
		$params [] = $device->device_no;
		return DB::update ( $sql, $params );
	}
}